<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>过滤器</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/dayjs/1.11.7/dayjs.min.js"></script>
</head>
<body>
    <div id="app">
        <h2>显示格式化后的时间</h2>

        <!-- 计算属性实现 -->
        <h3>现在是：{{ fmtTime }}</h3>

        <!-- methods实现 -->
        <h3>现在是：{{ getFmtTime() }}</h3>

        <!-- 过滤器实现 -->
        <h3>现在是：{{ time | timeFormater }}</h3>

        <!-- 过滤器实现（传参） -->
        <h3>现在是：{{ time | timeFormater('YYYY_MM_DD') | mySlice }}</h3>
        <h3 :x="msg | mySlice">Hello</h3>
    </div>

    <script>
        Vue.filter('mySlice', function (value) {
            return value.slice(0, 4)
        })
        new Vue({
            el: '#app',
            data: {
                time: Date.now(),
                msg: 'Hello World'
            },
            computed: {
                fmtTime() {
                    return dayjs(Date.now()).format('YYYY年MM月DD日 HH:mm:ss')
                }
            },
            filters: {
                timeFormater(value, format = 'YYYY年MM月DD日 HH:mm:ss') {
                    return dayjs(value).format(format)
                }
            },
            methods: {
                getFmtTime() {
                    return dayjs(Date.now()).format('YYYY年MM月DD日 HH:mm:ss')
                }
            }
        })
    </script>
</body>
</html>